package cn.cjx.basic.controller;

import cn.cjx.basic.dto.RegisterDto;
import cn.cjx.basic.exception.BusinessException;
import cn.cjx.basic.utils.AjaxResult;
import cn.cjx.basic.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: cjx
 * @Date: 2022/5/8
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

	@Autowired
	private IVerifyCodeService verifyCodeService;

	@GetMapping("/image/{key}")
	public AjaxResult imageCode(@PathVariable("key") String key) {
		try {
			String baseStr = verifyCodeService.imageCode(key);
			return AjaxResult.success().setResultObj(baseStr);
		}catch (BusinessException e) {//我们自己的业务错误  自定义异常
			e.printStackTrace();
			return AjaxResult.error().setMessage(e.getMessage());
		}catch (Exception e) {//系统级别的错误
			e.printStackTrace();
			return AjaxResult.error();
		}
	}


	@PostMapping("/sendMsgCode")
	public AjaxResult sendMsgCode(@RequestBody RegisterDto dto) {
		try {
			verifyCodeService.sendMsgCode(dto);
			return AjaxResult.success();
		}catch (BusinessException e) {//我们自己的业务错误  自定义异常
			e.printStackTrace();
			return AjaxResult.error().setMessage(e.getMessage());
		}catch (Exception e) {//系统级别的错误
			e.printStackTrace();
			return AjaxResult.error();
		}
	}


}
